package com.quectel.core.module.partybuildingactivity.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.partybuilding.dto.PartyPersonDto;
import com.quectel.core.module.partybuilding.service.PartyPersonService;
import com.quectel.core.module.partybuildingactivity.dao.PartyBuildingActivityPersonJoinBindingDao;
import com.quectel.core.module.partybuildingactivity.dto.PartyBuildingActivityDto;
import com.quectel.core.module.partybuildingactivity.dto.PartyBuildingActivityPersonJoinBindingDto;
import com.quectel.core.module.partybuildingactivity.entity.PartyBuildingActivityPersonJoinBindingEntity;
import com.quectel.core.module.partybuildingactivity.service.PartyBuildingActivityPersonJoinBindingService;
import com.quectel.core.module.partybuildingactivity.service.PartyBuildingActivityService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/24 11:38
 */
@DubboService
public class PartyBuildingActivityPersonJoinBindingServiceImpl implements PartyBuildingActivityPersonJoinBindingService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyBuildingActivityPersonJoinBindingServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> PARTY_BUILDING_ACTIVITY_Person_JOIN_BINDING_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "PARTY_BUILDING_ACTIVITY_Person_JOIN_BINDING_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<PartyBuildingActivityPersonJoinBindingDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(PARTY_BUILDING_ACTIVITY_Person_JOIN_BINDING_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private PartyBuildingActivityPersonJoinBindingDao partyBuildingActivityPersonJoinBindingDao;
    @Autowired
    private PartyBuildingActivityService partyBuildingActivityService;
    @Autowired
    private PartyPersonService partyPersonService;

    /**
     * @param activityId
     * @param personId
     * @return
     */
    @Override
    public PartyBuildingActivityPersonJoinBindingDto selectOne(Long activityId, Long personId) {
        PartyBuildingActivityPersonJoinBindingEntity entity = partyBuildingActivityPersonJoinBindingDao.selectOne(
                new LambdaQueryWrapper<PartyBuildingActivityPersonJoinBindingEntity>()
                        .eq(PartyBuildingActivityPersonJoinBindingEntity::getPartyBuildingActivityId, activityId)
                        .eq(PartyBuildingActivityPersonJoinBindingEntity::getPartyPersonId, personId)
        );
        return paddingField(CopyUtils.copyObj(entity, PartyBuildingActivityPersonJoinBindingDto.class));
    }

    /**
     * 根据活动id 查询关联的人员list
     *
     * @param id
     * @return
     */
    @Override
    public List<PartyBuildingActivityPersonJoinBindingDto> selectList(Long id) {
        List<PartyBuildingActivityPersonJoinBindingEntity> entityList = partyBuildingActivityPersonJoinBindingDao.selectList(
                new LambdaQueryWrapper<PartyBuildingActivityPersonJoinBindingEntity>()
                        .eq(PartyBuildingActivityPersonJoinBindingEntity::getPartyBuildingActivityId, id)
        );
        return CopyUtils.copyList(entityList, PartyBuildingActivityPersonJoinBindingDto.class);
    }

    @Override
    public PartyBuildingActivityPersonJoinBindingDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PARTY_BUILDING_ACTIVITY_Person_JOIN_BINDING_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PartyBuildingActivityPersonJoinBindingDto selectById(Long id){
            PartyBuildingActivityPersonJoinBindingEntity entity = partyBuildingActivityPersonJoinBindingDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,PartyBuildingActivityPersonJoinBindingDto.class));
    }

    @Override
    public List<PartyBuildingActivityPersonJoinBindingDto> queryList(Map<String,Object> params){
        List<PartyBuildingActivityPersonJoinBindingEntity> list = partyBuildingActivityPersonJoinBindingDao.queryList(params);
        List<PartyBuildingActivityPersonJoinBindingDto> result =  CopyUtils.copyList(list,PartyBuildingActivityPersonJoinBindingDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private PartyBuildingActivityPersonJoinBindingDto paddingField(PartyBuildingActivityPersonJoinBindingDto dto){
        if (dto != null){
            if (dto.getPartyBuildingActivityId()!=null){
                PartyBuildingActivityDto partyBuildingActivityDto = partyBuildingActivityService.selectCacheById(dto.getPartyBuildingActivityId());
                if (partyBuildingActivityDto!=null){
                    dto.setSubject(partyBuildingActivityDto.getSubject());
                }
            }
            if (dto.getPartyPersonId()!= null){
                PartyPersonDto personDto = partyPersonService.selectCacheById(dto.getPartyPersonId());
                if (personDto!= null){
                    dto.setPartyName(Objects.nonNull(personDto.getParty()) ? personDto.getParty().getName() : "");
                    dto.setPartyPersonName(personDto.getName());
                    dto.setMobile(personDto.getMobile());
                }
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return partyBuildingActivityPersonJoinBindingDao.queryTotal(params);
    }

    @Override
    public Long save(PartyBuildingActivityPersonJoinBindingDto dto){
        PartyBuildingActivityPersonJoinBindingEntity entity = CopyUtils.copyObj(dto,PartyBuildingActivityPersonJoinBindingEntity.class);
        entity.setId(Snowflake.nextId());
        partyBuildingActivityPersonJoinBindingDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PartyBuildingActivityPersonJoinBindingDto dto){
        PartyBuildingActivityPersonJoinBindingEntity entity = CopyUtils.copyObj(dto,PartyBuildingActivityPersonJoinBindingEntity.class);

        partyBuildingActivityPersonJoinBindingDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(PartyBuildingActivityPersonJoinBindingDto dto) {
       PartyBuildingActivityPersonJoinBindingEntity entity = CopyUtils.copyObj(dto,PartyBuildingActivityPersonJoinBindingEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, partyBuildingActivityPersonJoinBindingDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        partyBuildingActivityPersonJoinBindingDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
